package com.k.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.k.domain.User;
import com.k.mapper.UserMapper;
import com.k.service.UserService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: SpringSecote01
 * @ClassName UserServiceImpl
 * @description:
 * @author: xiaoJie
 * @create: 2024-09-11 20:05
 * @Version 1.0
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService, UserDetailsService {
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User one = lambdaQuery().eq(User::getUserCode, s).one();
        if (one == null) {
            throw new UsernameNotFoundException("账号不存在");
        }
        //在数据库查用户权限



        //存放用户权限和角色 用于后面拦截校验用户权限
        List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
        simpleGrantedAuthorities.add(new SimpleGrantedAuthority("ROLE_admin"));
        simpleGrantedAuthorities.add(new SimpleGrantedAuthority("system:user:add"));
        simpleGrantedAuthorities.add(new SimpleGrantedAuthority("system:user:del"));
        return new org.springframework.security.core.userdetails.User(one.getUserCode(),one.getPassword(), simpleGrantedAuthorities);
    }
}
